package com.pandora.credit.mongo.service.impl;

import com.pandora.common.util.Constants;
import com.pandora.common.util.JedisUtil;
import com.pandora.common.util.StringUtils;
import com.pandora.common.util.VerifyUtil;
import com.pandora.credit.dto.response.DetailResponse;
import com.pandora.credit.exception.auth.AuthorizationTokenInvalidException;
import com.pandora.credit.mongo.dao.MongoPandoraCreditDao;
import com.pandora.credit.mongo.entity.MongoPandoraCredit;
import com.pandora.credit.mongo.service.MongoPandoraCreditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author developer
 */
@Service
public class MongoPandoraCreditServiceImpl implements MongoPandoraCreditService {

    @Autowired
    private MongoPandoraCreditDao mongoPandoraCreditDao;

    @Override
    public List<MongoPandoraCredit> queryByIds(List<Long> ids) {
        return mongoPandoraCreditDao.selectByIds(ids);
    }

    @Override
    public MongoPandoraCredit queryByType(int type) {
        return mongoPandoraCreditDao.selectByType(type);
    }

    @Override
    public List<MongoPandoraCredit> queryByTypeIds(int type, List<Long> ids) {
        return mongoPandoraCreditDao.selectByTypeIds(type, ids);
    }

    @Override
    public MongoPandoraCredit selectById(long id) {
        return mongoPandoraCreditDao.selectById(id);
    }

    @Override
    public DetailResponse doGetDetail(String authorizationToken, long objId) {
        String mobile = JedisUtil.get(Constants.BUSINESS_REDIS_URL,
                StringUtils.getStrParam(Constants.AUTHORIZATION_TOKEN_KEY, authorizationToken));
        if(VerifyUtil.isEmpty(mobile)){
            throw new AuthorizationTokenInvalidException();
        }
        MongoPandoraCredit itemMongo = selectById(objId);
        String typeStr = "";
        if(itemMongo != null){
            typeStr = StringUtils.returnTypeFromLabel(itemMongo.getType());
        }
        return new DetailResponse(typeStr, itemMongo);
    }
}
